package com.hiultra.archive.controller;

import com.hiultra.archive.domain.ArcDossier;
import com.hiultra.archive.service.IArcCatalogService;
import com.hiultra.archive.service.IArcDossierService;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.core.domain.TreeSelect;
import com.hiultra.common.core.domain.entity.ArcCatalog;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.common.utils.StringUtils;
import com.hiultra.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分类Controller
 *
 * @author ls
 * @date 2022-07-28
 */
@Api(tags = "分类Controller")
@RestController
@RequestMapping("/base/catalog")
public class ArcCatalogController extends BaseController {
    @Autowired
    private IArcCatalogService arcCatalogService;
    @Autowired
    private IArcDossierService arcDossierService;

    /**
     * 查询分类列表
     */
    @ApiOperation(value = "查询分类列表", notes = "查询分类列表")
    @PreAuthorize("@ss.hasPermi('base:catalog:list')")
    @GetMapping("/list")
    public AjaxResult list(ArcCatalog arcCatalog) {
        List<ArcCatalog> list = arcCatalogService.selectArcCatalogList(arcCatalog);
        return AjaxResult.success(list);
    }

    /**
     * 导出分类列表
     */
    @ApiOperation(value = "导出分类列表", notes = "导出分类列表")
    @PreAuthorize("@ss.hasPermi('base:catalog:export')")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArcCatalog arcCatalog) {
        List<ArcCatalog> list = arcCatalogService.selectArcCatalogList(arcCatalog);
        for (ArcCatalog catalog : list) {
            if (0 != catalog.getParentId()) {
                ArcCatalog arcCatalog1 = arcCatalogService.selectArcCatalogByCatalogId(catalog.getParentId());
                catalog.setParentName(arcCatalog1.getCatalogName());
            }
        }
        //TODO 树结构导出后的顺序不对
        //List<TreeSelect> treeSelects = arcCatalogService.buildCatalogTreeSelect(list);
        //for (TreeSelect treeSelect : treeSelects) {
        //    System.out.println(treeSelect);
        //}
        //ExcelUtil<TreeSelect> util = new ExcelUtil<TreeSelect>(TreeSelect.class);
        //util.exportExcel(response, treeSelects, "分类数据");
        ExcelUtil<ArcCatalog> util = new ExcelUtil<ArcCatalog>(ArcCatalog.class);
        util.exportExcel(response, list, "分类数据");
    }
    //烟台不是员工电脑   青岛      一体机刚到货
    /**
     * 获取分类详细信息
     */
    @ApiOperation(value = "获取分类详细信息", notes = "获取分类详细信息")
    @PreAuthorize("@ss.hasPermi('base:catalog:query')")
    @GetMapping(value = "/{catalogId}")
    public AjaxResult getInfo(@PathVariable("catalogId") Long catalogId) {
        return AjaxResult.success(arcCatalogService.selectArcCatalogByCatalogId(catalogId));
    }

    /**
     * 新增分类
     */
    @ApiOperation(value = "新增分类", notes = "新增分类")
    @PreAuthorize("@ss.hasPermi('base:catalog:add')")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArcCatalog arcCatalog) {
        return toAjax(arcCatalogService.insertArcCatalog(arcCatalog));
    }

    /**
     * 修改分类
     */
    @ApiOperation(value = "修改分类", notes = "修改分类")
    @PreAuthorize("@ss.hasPermi('base:catalog:edit')")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArcCatalog arcCatalog) {
        return toAjax(arcCatalogService.updateArcCatalog(arcCatalog));
    }

    /**
     * 删除分类
     */
    @PreAuthorize("@ss.hasPermi('base:catalog:remove')")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{catalogIds}")
    @ApiOperation(value = "删除分类", notes = "删除分类")
    public AjaxResult remove(@PathVariable Long[] catalogIds) {
        for (Long catalogId : catalogIds) {
            //判断分类是否被引用
            List<ArcDossier> dossierList = arcDossierService.selectArcDossierList(new ArcDossier() {{
                setCatalogId(catalogId);
            }});
            if (dossierList.size() > 0) {
                ArcCatalog arcCatalog = arcCatalogService.selectArcCatalogByCatalogId(catalogId);
                return AjaxResult.error("分类" + arcCatalog.getCatalogName() + "被引用，无法删除！");
            }
        }
        return toAjax(arcCatalogService.deleteArcCatalogByCatalogIds(catalogIds));
    }

    /**
     * 获取分类下拉树列表
     */
    @ApiOperation(value = "导出智能柜列表", notes = "导出智能柜列表")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(ArcCatalog arcCatalog) {
        List<ArcCatalog> catalogs = arcCatalogService.selectArcCatalogList(arcCatalog);
        return AjaxResult.success(arcCatalogService.buildCatalogTreeSelect(catalogs));
    }

    /**
     * 获取分类导入
     */
    @ApiOperation(value = "分类导入", notes = "分类导入")
    @Log(title = "分类导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('base:catalog:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ArcCatalog> util = new ExcelUtil<ArcCatalog>(ArcCatalog.class);
        List<ArcCatalog> catalogList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = arcCatalogService.importCatalog(catalogList, updateSupport, operName);
        return AjaxResult.success(message);
        //return AjaxResult.success();
    }

    @ApiOperation(value = "分类模板", notes = "分类模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ArcCatalog> util = new ExcelUtil<ArcCatalog>(ArcCatalog.class);
        util.importTemplateExcel(response, "分类数据");
    }
}
